<?php
/**
 * author by OCY, 2022/01/19 16:09.
 */

namespace app\index\controller;

use app\common\JwtHelper;
use app\middleware\SecurityMiddleware;
use think\Controller;
use think\Db;
use think\Request;

class Callback extends Controller
{

    public function __construct()
    {
        parent::__construct();

        // 调用中间件
        $middleware = new SecurityMiddleware();
        $request = Request::instance();
        $middleware->handle($request, function ($req) {
            // 仅执行验证，不做中间件链路回调
            return response(); // 返回空响应对象用于中间件添加响应头
        });
    }

    /**
     * 发起课程
     * */
    public function courseCreate()
    {
        $input = Request()->param();

        $course_id = $input['course_id'];
        $teacher_id = $input['teacher_id'];
        $course_title = $input['course_title'];
        $course_price = $input['course_price'];
        $start_time = $input['start_time'];
        $end_time = $input['end_time'];

        if (empty($course_id)) apiReturn(500, '缺少课程ID');
        if (empty($teacher_id)) apiReturn(500, '缺少老师ID');
        if (empty($course_title)) apiReturn(500, '缺少课程名称');
        if ($course_price <= 0 || $course_price != intval($course_price)) apiReturn(500, '课程价格错误');
        if (empty($start_time)) apiReturn(500, '缺少计划开课时间');
        if (empty($end_time)) apiReturn(500, '缺少计划结束时间');

        $count = Db::name('course')->where('course_id', $course_id)->count();
        if ($count > 0) apiReturn(500, '课程已存在');

        $data = [
            'course_id' => $course_id,
            'teacher_id' => $teacher_id,
            'course_title' => $course_title,
            'course_price' => $course_price,
            'start_time' => $start_time,
            'end_time' => $end_time,
            'create_time' => time()
        ];

        $res = Db::name('course')->insert($data);
        if ($res) {
            apiReturn(200, '课程创建成功');
        } else {
            apiReturn(500, '课程创建失败');
        }
    }

    /**
     * 修改课程开课时间
     * */
    public function courseUpdate()
    {
        $input = Request()->param();

        $course_id = $input['course_id'];
        $start_time = $input['start_time'];
        $end_time = $input['end_time'];
        $reason = $input['reason'];

        if (empty($course_id)) apiReturn(500, '缺少课程ID');
        if (empty($start_time)) apiReturn(500, '缺少计划开课时间');
        if (empty($end_time)) apiReturn(500, '缺少计划结束时间');

        $info = Db::name('course')->where('course_id', $course_id)->find();
        if (empty($info)) apiReturn(500, '课程不存在');
        if ($info['status'] != 1) apiReturn(500, '课程状态不正确');

        $data = [
            'start_time' => $start_time,
            'end_time' => $end_time,
            'update_reason' => $reason,
            'update_time' => time()
        ];
        $res = Db::name('course')->where('course_id', $course_id)->update($data);

        if ($res) {
            apiReturn(200, '修改课程开课时间成功');
        } else {
            apiReturn(500, '修改课程开课时间失败');
        }
    }

    /**
     * 课程取消
     * */
    public function courseCancel()
    {
        $input = Request()->param();

        $course_id = $input['course_id'];
        $reason = $input['reason'];

        if (empty($course_id)) apiReturn(500, '缺少课程ID');

        $count = Db::name('course')->where('course_id', $course_id)->count();
        if ($count == 0) apiReturn(500, '课程不存在');

        $data = [
            'status' => -1,
            'cancel_reason' => $reason
        ];
        $res = Db::name('course')->where('course_id', $course_id)->update($data);
        if ($res) {
            apiReturn(200, '课程取消成功');
        } else {
            apiReturn(500, '课程取消失败');
        }
    }

    /**
     * 学生课程下单
     * */
    public function orderCreate()
    {
        $input = Request()->param();

        $order_sn = $input['order_sn'];
        $trade_no = $input['trade_no'] ? : '';
        $out_trade_no = $input['out_trade_no'] ? : '';
        $course_id = $input['course_id'];
        $teacher_id = $input['teacher_id'];
        $student_id = $input['student_id'];
        $student_name = $input['student_name'];
        $order_amount = $input['order_amount'];
        $payment_amount = $input['payment_amount'];
        $payment_method = $input['payment_method'];

        if (empty($order_sn)) apiReturn(500, '缺少订单号');
        if (empty($trade_no) && empty($out_trade_no)) apiReturn(500, '缺少交易单号');
        if (empty($course_id)) apiReturn(500, '缺少课程ID');
        if (empty($teacher_id)) apiReturn(500, '缺少老师ID');
        if (empty($student_id)) apiReturn(500, '缺少学生ID');
        if (empty($student_name)) apiReturn(500, '缺少学生姓名');
        if ($order_amount <= 0 || $order_amount != intval($order_amount)) apiReturn(500, '订单金额错误');
        if ($payment_amount < 0 || $payment_amount != intval($payment_amount)) apiReturn(500, '支付金额错误');
        if (empty($payment_method)) apiReturn(500, '缺少支付方式');

        $data = [
            'order_sn' => $order_sn,
            'trade_no' => $trade_no,
            'out_trade_no' => $out_trade_no,
            'course_id' => $course_id,
            'teacher_id' => $teacher_id,
            'student_id' => $student_id,
            'student_name' => $student_name,
            'order_amount' => $order_amount,
            'payment_amount' => $payment_amount,
            'payment_method' => $payment_method,
            'status' => 1,
            'create_time' => time()
        ];
        $res = Db::name('order')->insert($data);
        if ($res) {
            apiReturn(200, '订单创建成功');
        } else {
            apiReturn(500, '订单创建失败');
        }
    }

    /**
     * 申请退款
     * */
    public function refundApply()
    {
        $input = Request()->param();

        $order_sn = $input['order_sn'];
        $applied_amount = $input['applied_amount'];
        $refund_reason = $input['refund_reason'];

        if (empty($order_sn)) apiReturn(500, '缺少订单号');
        if ($applied_amount <= 0 || $applied_amount != intval($applied_amount)) apiReturn(500, '退款金额错误');

        $info = Db::name('order')->where('order_sn', $order_sn)->find();
        if (empty($info)) apiReturn(500, '订单不存在');
        if ($info['status'] != 1) apiReturn(500, '订单状态错误');

        $data = [
            'status' => 2,
            'refund_amount' => $applied_amount,
            'refund_reason' => $refund_reason,
            'update_time' => time()
        ];
        $res = Db::name('order')->where('order_sn', $order_sn)->update($data);
        if ($res) {
            apiReturn(200, '申请退款成功，请等待审核');
        } else {
            apiReturn(500, '申请退款失败');
        }
    }
}